package com.complaint.dao.impl;

import com.complaint.dao.BaseDAO;
import com.complaint.dao.UserDAO;
import com.complaint.pojo.Complaint;
import com.complaint.pojo.User;

import java.util.List;

/**
 * UserDAOImpl 是UserDAO的实例类
 *
 * */
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public int add(User user) {
        String sql = "insert into userTable values(DEFAULT, ?, ?)";
        return update(sql, user.getName(), user.getPassword());
    }

    public List<User> findByName(String username) {
        String sql = "select password from userTable where name=?";
        List<User> list = sQuery(User.class, sql, username);
        return list;
    }

    @Override
    public int findId(String username) {
        String sql = "select id from userTable where name=?";
        return (int) aQuery(Integer.class, sql, username);
    }

    public List<Complaint> findForMain(int id) {
        String sql = "select * from complaintTable where userId=?";
        return sQuery(Complaint.class, sql, id);
    }

    @Override
    public int removeUser(String id) {
        String sql = "delete from userTable where id = ?";
        return update(sql, id);
    }
}
